﻿using UnityEngine;
using Holoville.HOTween;
using System.Collections;

public class TweenNumber : MonoBehaviour
{
	public float delay;
	public float dur;
	public int indexCode;

	UILabel label;
	Tweener tweenNum;
	[HideInInspector]
	public float desNum;
	int curNum = 0;
	void Start()
	{
		label = GetComponent<UILabel>();
		if (label == null)
		{
			return;
		}
	}

	public void Play(string code)
	{
		if (code[indexCode] == '0')
		{
			return;
		}
		if (label == null)
		{
			label = GetComponent<UILabel>();
		}
		if (label == null)
		{
			return;
		}
		if (gameObject.active == false)
		{
			return;
		}
		if (!int.TryParse(label.text,out curNum))
		{
			return;
		}
		label.text = "";
		StartCoroutine(RunLikeGame(delay, curNum, label));
	}

	private IEnumerator RunLikeGame(float p, int numDes, UILabel lbUI)
	{
		yield return new WaitForSeconds(p);
		if (tweenNum != null)
		{
			tweenNum.Complete();
			tweenNum.Kill();
		}

		desNum = 0;
		tweenNum = HOTween.To(this, dur, new TweenParms().Prop("desNum", numDes).OnUpdate(UpdateNum, lbUI).OnComplete(OnComplete));
	}

	void UpdateNum(TweenEvent p_callbackData)
	{
		((UILabel)p_callbackData.parms[0]).text = DUtil.GetStringFormatNum((int)desNum);
	}

	void OnComplete()
	{
		label.text = DUtil.GetStringFormatNum(curNum);
	}
}